package com.bwie.config;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisConfig {

    //连接redis数据库
    public static JedisPool redisConn() {
        JedisPoolConfig config = new JedisPoolConfig(); // 连接池的配置对象
        config.setMaxTotal(100); // 设置最大连接数
        config.setMaxIdle(10); // 设置最大空闲连接数
        String host = "127.0.0.1";
        int port = 6379;
        String password = "123456"; // 这里替换为你的Redis密码
        JedisPool jedisPool = new JedisPool(config, host, port, 2000);
        try {
            return jedisPool;
        } catch (Exception exception) {
            throw new RuntimeException(exception.getMessage());
        }
    }

    //关闭
    public static void closeJedisOrPool(Jedis jedis, JedisPool jedisPool) {
        if (null != jedis) {
            jedis.close();
        }
        if (null != jedisPool) {
            jedisPool.close();
        }
    }

    public static void main(String[] args) {
        // 获取spring应用上下文
        JedisPool jedisPool = redisConn();
        //获取jedis实例
        Jedis jedis = jedisPool.getResource();

        //5中数据结构
        //string
//        jedis.set("name","aa");
//        System.out.println("字符串:"+jedis.get("name"));
//        System.out.println("字符串是否存在:"+jedis.exists("name111"));
//        System.out.println("字符串长度:"+jedis.strlen("name"));

        //重命名
        //jedis.rename("name", "name2");


        //jedis.mset("key1", "val1", "key2", "val2", "key3", "100");
        //System.out.println( "批量取值"+jedis.mget("key1", "key2" ,"key3"));


        //jedis.set("count","0");
        // System.out.println("递增："+jedis.incr("count"));

        //jedis.setex("name", 5, "sss");//单位秒


        //list

        //jedis.lpush("list","1","2","3","4","5");
        //System.out.println("获取list"+jedis.lrange("list",0,1));


        //hash{map01,{id,1}}
        //jedis.hset("map01","name","aaa");//存
//        System.out.println("hashmap01=name:"+jedis.hget("map01","name"));
//        System.out.println("hashmap01:"+jedis.hgetAll("map01"));
//
//        jedis.hset("map002","userId001","0");
//        System.out.println("userId001积分:"+jedis.hget("map002","userId001"));
//

        // jedis.hincrBy("map002","userId001",1);//自增
        //System.out.println("userId001积分:"+jedis.hget("map002","userId001"));


//        String hget = jedis.hget("map002", "userId001");
//        Integer integer = Integer.valueOf(hget);
//        if(integer>0){//
//            jedis.hincrBy("map002","userId001",-1);//自减
//            System.out.println("userId001积分减:"+jedis.hget("map002","userId001"));
//
//        }

        // sort set

//        jedis.zadd("sset",4000,"1");
//        jedis.zadd("sset",200,"2");
//        jedis.zadd("sset",80,"4");
//        jedis.zadd("sset",600,"5");
//        jedis.zadd("sset",5000,"6");
//        jedis.zadd("sset",55,"7");

        // System.out.println("倒序 " + jedis.zrevrange("sset", 0, 1));

        //set

//        jedis.sadd("wxset001","1","3","4","5");
//        jedis.sadd("wxset002","2","200","4","10");

        //System.out.println("用户2有没有关注001用户:"+jedis.sismember("wxset002", "4"));


        closeJedisOrPool(jedis, jedisPool);
    }


}